const { log } = require('winston');
const {getOrder} =require('../controllers/payController');
const { EPayPageExecuteResFormat, ePaySDK } =require('../utils/pay') 


/** 生成支付链接 */
const generatePay = async (req) => {
  const body = req.body;
  const order = await getOrder({
    outTradeNo:req.body.outTradeNo,
    userId:req.userId
  })
  const { outTradeNo, totalAmount, subject } = order
  const bizContent = {
    type: req.body.type,
    out_trade_no: outTradeNo,
    money: totalAmount,
    name: subject,
  }
  const data = await ePaySDK.pageExecute(+body.resFormat || 1, {
    bizContent,
    return_url: process.env.EZF_RETURN_URL ,
    notify_url: process.env.EZF_NOTIFY_URL ,
  })
  console.log(data)
  if (data.code !== 200) {
    throw new InternalServerError(data.msg)
  }
  return data
}


module.exports = {
  getOrder,
  generatePay
}